package com.jiejie.university.controller;


import cn.hutool.core.bean.BeanUtil;
import com.jiejie.university.entity.Manage;
import com.jiejie.university.service.ManageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/manage")
public class ManageController {
    @Resource
    private ManageService manageService;

    @GetMapping("/loginManage")
    public ModelAndView loginManage(){
        return new ModelAndView("login");
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(@RequestParam String username, @RequestParam String password) {
        ModelAndView mav = new ModelAndView();
        Manage info = manageService.login(username, password);
        if (BeanUtil.isEmpty(info)) {
            mav.setViewName("redirect:/manage/registerManage");
        } else {

            mav.setViewName("redirect:/student/findStudentAll");
        }

        return mav;
    }

    /**
     * 注册
     * @param model
     * @return
     */
    @GetMapping("/registerManage")
    public ModelAndView registerManage(Model model){
        model.addAttribute("manage",new Manage());
        return new ModelAndView("register","manmodel",model);
    }
    @PostMapping("/register")
    @Transactional
    public ModelAndView register(Manage manage, Model model){
        ModelAndView mav = new ModelAndView();
        int result = manageService.register(manage);
        if (result == 200) {
            mav.setViewName("login");
        } else {
            mav.setViewName("register");
        }
        return mav;
    }

}
